package com.heima.wemedia.service.impl;


import com.alibaba.fastjson.JSON;
import com.heima.apis.schedule.ITaskClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.Task;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmNewsTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class WmNewsTaskServiceImpl  implements WmNewsTaskService {

    @Autowired
    private ITaskClient iTaskClient;

    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;

    /**
     * todo 消费立即队列进行审核
     */
    //@Scheduled(cron = "* * * * * ?")
    @Override
    public void scanNewsByTask() {
        //1.todo 执行POLL方法获得任务对象（文章ID）
        ResponseResult result = iTaskClient.poll(250, 250);
        if(result.getCode() == 200 && result.getData() != null){
            System.out.println("请求获得的数据是:"+result.getData());
            //1.todo 把map。转换成 json字符串，2.把json字符串转换成对象
            Task task = JSON.parseObject(JSON.toJSONString(result.getData()) ,Task.class) ;
            WmNews wmNews = JSON.parseObject(task.getParameters(), WmNews.class);
            //2.todo 调用自动审核接口审核文章
            wmNewsAutoScanService.autoScanWmNews(wmNews.getId());
            System.out.println("开始审核文章，"+ wmNews.getId());
        }
    }
}
